package test.marui.com.mytest.ui.cntenSDKDemo;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.cnten.bdcloudrtk.gnss.RTKMainActivity;
import com.cnten.bdcloudrtk.gnss.RTKService;
import com.cnten.bdcloudrtk.gnss.manager.DeviceManager;

import test.marui.com.mytest.R;

public class CntenSDKDemoActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView mLat,mLng,mCityX,mCityY;
    private Button mDeviceConnection,mCityCoordinateConvert;
    private RTKService mRTKService;
    private static final int REFRESH_LOCATION = 0x001024;
    private boolean isStopLocate = false;
    private boolean isCityConvert = false;
    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case REFRESH_LOCATION:
                    if(RTKService.latitude == 0 || RTKService.longitude ==0){
                        mLat.setText(getResources().getString(R.string.please_wait));
                        mLng.setText(getResources().getString(R.string.no_value));
                    }else{
                        mLat.setText("LAT：" + String.valueOf(RTKService.latitude));
                        mLng.setText("LNG：" + String.valueOf(RTKService.longitude));
                        mCityX.setText("X：" + String.valueOf(RTKService.cityX));
                        mCityY.setText("Y：" + String.valueOf(RTKService.cityY));
                    }
                    break;

                default:
                    break;
            }
        };
    };

    private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mRTKService = ((RTKService.RTKServiceBinder) service).getService();
            //是否用北斗定位设备，只有设置此项的时候，如果想要获取精准位置信息，需打开此项
            mRTKService.useBDLocation(true);
        }
        @Override
        public void onServiceDisconnected(ComponentName name) {
            mRTKService = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_bd);
        initData();
        initView();
    }

    private void initData(){
        Intent intent = new Intent(CntenSDKDemoActivity.this, RTKService.class);
        bindService(intent, mConnection, BIND_AUTO_CREATE);
        new RTKThread().start();
    }

    private void initView(){
        mLat = findViewById(R.id.lat);
        mLng = findViewById(R.id.lng);
        mCityX = findViewById(R.id.city_x);
        mCityY = findViewById(R.id.city_y);
        mDeviceConnection = findViewById(R.id.device_connection);
        mCityCoordinateConvert = findViewById(R.id.city_coordinate_convert);
        mDeviceConnection.setOnClickListener(this);
        mCityCoordinateConvert.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.device_connection){
            Intent leaIntent = new Intent(CntenSDKDemoActivity.this, RTKMainActivity.class);
            startActivity(leaIntent);
        }else if(id == R.id.city_coordinate_convert){
            isCityConvert = !isCityConvert;
            //城建坐标转换开关
            mRTKService.useCityCoordinate(isCityConvert);
        }

    }


    class RTKThread extends Thread{
        @Override
        public void run() {
            super.run();
            try {
                while (!isStopLocate) {
                    Thread.sleep(1000);
                    mHandler.sendEmptyMessage(REFRESH_LOCATION);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //作业完毕的时候，需要将定位 Service 关掉，同时断开蓝牙连接
        isStopLocate = true;
        Intent bindIntent = new Intent(this, RTKService.class);
        unbindService(mConnection);
        stopService(bindIntent);
        DeviceManager.getInstance().destroy();
    }
}
